﻿using Macro_stone.PoModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SystemCenterSoa.DtoModel.Common;
using SystemCenterSoa.IRepository.SystemManager;
using SystemCenterSoa.Repository.CommonLib;

namespace SystemCenterSoa.Repository.SystemManager
{
    public class DepartmentStaffRepository : BaseRepository, IDepartmentStaffRepository
    {
        public DepartmentStaffRepository(SystemCenterDbSoaContext SystemCenterDbSoaContext) : base(SystemCenterDbSoaContext)
        {

        }
        /// <summary>
        /// 部门—显示
        /// </summary>
        /// <param name="searchModel"></param>
        /// <returns></returns>
        public List<DepartmentPo> DepartmentPoInquire(int pageIndex,int pageSize, string deptName)
        {
            var shuju = _systemCenterDbSoaContext.DepartmentPo.AsQueryable();
            shuju = shuju.Where(p => p.depDepartment > 0);


                if (!string.IsNullOrWhiteSpace(deptName))
                {
                    //部门名称
                    shuju = shuju.Where(p => p.deptName.Contains(deptName));
                }
            int ssa = (pageIndex - 1) * pageSize;
            List<DepartmentPo> sssa = shuju.OrderBy(p => p.staffId).Skip(ssa).Take(pageSize).ToList();
            return sssa;
        }

        /// <summary>
        /// 添加部门
        /// </summary>
        /// <param name="view"></param>
        /// <returns></returns>
        public bool DepartmentPoAdd(DepartmentPo view)
        {
            var ssa = _systemCenterDbSoaContext.DepartmentPo.FirstOrDefault(p => p.deptId == view.depDepartment);
            view.depDepartName = ssa.deptName;
            view.deptIndex = 1;
            _systemCenterDbSoaContext.DepartmentPo.Add(view);
            return _systemCenterDbSoaContext.SaveChanges() > 0;
        }

        /// <summary>
        /// 部门反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public DepartmentPo DepartmentPoOne(int id)
        {
            return _systemCenterDbSoaContext.DepartmentPo.FirstOrDefault(p => p.deptId == id);
        }

        /// <summary>
        /// 部门修改
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DepartmentPoUpdate(DepartmentPo view)
        {
            _systemCenterDbSoaContext.Entry(view).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            return _systemCenterDbSoaContext.SaveChanges() > 0;
        }

        /// <summary>
        /// 部门删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DepartmentPoDelete(int id)
        {
            var ssa= _systemCenterDbSoaContext.DepartmentPo.FirstOrDefault(p => p.deptId == id);
            _systemCenterDbSoaContext.DepartmentPo.Remove(ssa);
            return _systemCenterDbSoaContext.SaveChanges() > 0;
        }

        /// <summary>
        /// 人员管理—显示
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="buming"></param>
        /// <param name="name"></param>
        /// <param name="zhuang"></param>
        /// <returns></returns>
        public List<StaffDto> staffPoIndex(int pageIndex,int pageSize,string buming, string name,int? zhuang)
        {
            var shuju = from A in _systemCenterDbSoaContext.StaffPo
                        join B in _systemCenterDbSoaContext.DepartmentPo
                        on A.deptId equals B.deptId
                        select new StaffDto()
                        {
                            staffId = A.staffId,
                            loginName = A.loginName,
                            staffName = A.staffName,
                            staffTime = A.staffTime,
                            sex = A.sex,
                            deptId = A.deptId,
                            deptName = B.deptName,
                            majorId = A.majorId,
                            roleId = A.roleId,
                            passWord = A.passWord,
                            email = A.email,
                            phone = A.phone,
                            telephone = A.telephone,
                            professionalId = A.professionalId,
                            managementId = A.managementId,
                            technologyId = A.technologyId,
                            state = A.state,
                            img = A.img,
                            order = A.order,
                            entryTime = A.entryTime,
                            resignationTime = A.resignationTime
                        };
            if (!string.IsNullOrWhiteSpace(buming))
            {
                shuju = shuju.Where(p => p.deptName.Contains(buming));
            }
            if(!string.IsNullOrWhiteSpace(name))
            {
                shuju = shuju.Where(p => p.staffName.Contains(name));
            }
            if(zhuang !=null && zhuang>0)
            {
                shuju = shuju.Where(p => p.state == zhuang);
            }
                int ssa = (pageIndex - 1) * pageSize;
            List<StaffDto> coude = shuju.OrderBy(p => p.staffId).Skip(ssa).Take(pageSize).ToList();
            //throw new NotImplementedException();
            return coude;
        }

        /// <summary>
        /// 人员管理—添加
        /// </summary>
        /// <param name="view"></param>
        /// <returns></returns>
        public bool StaffPoAdd (StaffPo view)
        {
            _systemCenterDbSoaContext.StaffPo.Add(view);
            return _systemCenterDbSoaContext.SaveChanges() > 0;
        }

        /// <summary>
        /// 人员管理—删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool StaffPoDelete (int id)
        {
            var ssa = _systemCenterDbSoaContext.StaffPo.Find(id);
            _systemCenterDbSoaContext.StaffPo.Remove(ssa);
            return _systemCenterDbSoaContext.SaveChanges() > 0;
        }

        /// <summary>
        /// 部门下拉
        /// </summary>
        /// <returns></returns>
        public List<DepartmentPo> departmentPoXia()
        {
            var shuju = _systemCenterDbSoaContext.DepartmentPo.AsQueryable();
            shuju = shuju.Where(p => p.depDepartment > 0);
            return shuju.ToList();
        }
    }
}
